package com.tjvib.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;

import com.tjvib.common.Callback;
import com.tjvib.service.SensorService;
import com.tjvib.util.LogUtil;
import com.tjvib.util.ToastUtil;
import com.tjvib.view.activity.SensorActivity;

import java.util.Timer;
import java.util.TimerTask;

public abstract class SensorPresenter<T extends SensorService, V extends SensorActivity> extends BasePresenter {

    static final int READ_DATA_RATE = 10;

    MyResultReceiver resultReceiver;
    V view;
    T mService;
    boolean bound = false;
    Timer timer = new Timer();

    public SensorPresenter(V view) {
        this.view = view;
        resultReceiver = new MyResultReceiver(null);
    }

    public void startOrBindToService() {
        if (!mService.isRunning) {
            Intent intent = new Intent(view, getServiceClass());
            intent.putExtra("receiver", resultReceiver);
            view.startService(intent);
        }
        bindToService();
    }

    public void bindToService() {
        Intent intent = new Intent(view, getServiceClass());
        view.bindService(intent, getServiceConnection(), Context.BIND_AUTO_CREATE);
    }

    public void unbindFromService() {
        if (bound) {
            view.unbindService(getServiceConnection());
            bound = false;
        }
    }

    public void stopService() {
        if (mService != null)
            mService.disconnectAllSensor();
        Intent intent = new Intent(view, getServiceClass());
        view.stopService(intent);
    }

    public void startReadingData() {
        timer.scheduleAtFixedRate(getReadDataTask(), 0, READ_DATA_RATE);
    }

    public void stopReadingData() {
        timer.cancel();
    }

    public void startRecording() {
        mService.resetTimestamp();
        mService.startRecording();
    }

    public void stopRecording() {
        mService.stopRecording();
    }

    public boolean isRecording() {
        return mService.isRecording();
    }

    public abstract ServiceConnection getServiceConnection();

    public abstract Class<?> getServiceClass();

    public abstract TimerTask getReadDataTask();

    public abstract void setStreamFreq(int freq);

    public abstract int getStreamFreq();

    public abstract void discardData();

    public abstract void storeData(String dataName, Callback callback);

    public abstract void resetTimestamp();

    public abstract String getDataSetType();

    public boolean isBound() {
        return bound;
    }

    class MyResultReceiver extends ResultReceiver {
        public MyResultReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            if (resultCode == SensorService.MSG_SENSOR_CONNECTED) {
                ToastUtil.show(resultData.getString(SensorService.KEY_MESG));
            } else if (resultCode == SensorService.MSG_SENSOR_DISCONNECTED) {
                ToastUtil.show(resultData.getString(SensorService.KEY_MESG));
            } else if (resultCode == SensorService.MSG_SENSOR_CONNECTION_ERROR) {
                ToastUtil.show(resultData.getString(SensorService.KEY_MESG));
            } else {
                //showToast("yay");
            }
        }
    }

}
